package pers.lx.crm.service.business.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pers.lx.crm.base.common.pigeon.Result;
import pers.lx.crm.base.constant.ErrorConstant;
import pers.lx.crm.base.util.ResultUtil;
import pers.lx.crm.dao.business.FlowDao;
import pers.lx.crm.entiy.business.FlowDO;
import pers.lx.crm.service.business.FlowService;

import java.util.List;

/**
 * Created by lx on 2017/4/25.
 */
@Service
public class FlowServiceImpl implements FlowService {

    @Autowired
    private FlowDao flowDao ;

    @Override
    public Result getFlows(FlowDO flowDO) {
        Result result = new Result() ;
        try {
            flowDO.setList(flowDao.selectFlowListPage(flowDO));
            return ResultUtil.getPageResult(flowDO,result);
        } catch (NullPointerException e) {
            e.printStackTrace();
            result.setError(ErrorConstant.PARAMETER_NULL);
        } catch (Exception e){
            e.printStackTrace();
            result.setError(ErrorConstant.UNKNOWN_ERROR);
        }
        return result;
    }

    @Override
    public Result addFlow(FlowDO flowDO) {
        Result result = new Result() ;
        try {
            flowDao.insertFlow(flowDO);
        } catch (Exception e){
            e.printStackTrace();
            result.setError(ErrorConstant.UNKNOWN_ERROR);
        }
        return result;
    }

    @Override
    public Result updateFlow(FlowDO flowDO) {
        Result result = new Result() ;
        try {
            flowDao.updateFlow(flowDO);
        } catch (Exception e){
            e.printStackTrace();
            result.setError(ErrorConstant.UNKNOWN_ERROR);
        }
        return result;
    }

    @Transactional
    @Override
    public Result removeFlows(List<FlowDO> flowDOs) {
        Result result = new Result() ;
        for(FlowDO flowDo:flowDOs){
            flowDao.deleteFlow(flowDo);
        }
        return result;
    }
}
